package com.ruoyi.web.controller.an;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.an.domain.LoginLog;
import com.ruoyi.an.service.ILoginLogService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.web.controller.common.CommonUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 登录日志Controller
 * 
 * @author ruoyi
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/an/loginlog")
public class LoginLogController extends BaseController
{
    @Autowired
    private ILoginLogService loginLogService;

    /**
     * 查询登录日志列表
     */
//    @PreAuthorize("@ss.hasPermi('system:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoginLog loginLog)
    {
        startPage();
        List<LoginLog> list = loginLogService.selectLoginLogList(loginLog);
        return getDataTable(list);
    }

    /**
     * 导出登录日志列表
     */
//    @PreAuthorize("@ss.hasPermi('system:log:export')")
//    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoginLog loginLog)
    {
        List<LoginLog> list = loginLogService.selectLoginLogList(loginLog);
        ExcelUtil<LoginLog> util = new ExcelUtil<LoginLog>(LoginLog.class);
        util.exportExcel(response, list, "登录日志数据");
    }

    /**
     * 获取登录日志详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(loginLogService.selectLoginLogById(id));
    }

    /**
     * 新增登录日志
     */
//    @PreAuthorize("@ss.hasPermi('system:log:add')")
//    @Log(title = "登录日志", businessType = BusinessType.INSERT)
    @Anonymous
    @PostMapping("/add")
    public AjaxResult add(HttpServletRequest request, @RequestBody LoginLog loginLog)
    {
        String ipAddress = CommonUtils.getIpAddress(request);
        loginLog.setCreateTime(new Date());
        loginLog.setIpAdress(ipAddress);
        return toAjax(loginLogService.insertLoginLog(loginLog));
    }

    /**
     * 修改登录日志
     */
//    @PreAuthorize("@ss.hasPermi('system:log:edit')")
//    @Log(title = "登录日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoginLog loginLog)
    {
        return toAjax(loginLogService.updateLoginLog(loginLog));
    }

    /**
     * 删除登录日志
     */
//    @PreAuthorize("@ss.hasPermi('system:log:remove')")
//    @Log(title = "登录日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(loginLogService.deleteLoginLogByIds(ids));
    }
}
